﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class Admin_User_wxUserAdd : System.Web.UI.Page
{
    public int _Id = 0;
    public string device_power = "", _ID = "", miao_power = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.CacheControl = "no-cache";
        if (!string.IsNullOrEmpty(Request.Params["ID"]))
        {
            _ID = HttpContext.Current.Request.QueryString["ID"];
            _Id = int.Parse(_ID);
        }
        if (!IsPostBack)
        {
            //ClassTreeBind("一级分类...", this.ddlEmployee);

            Button1.Attributes.Add("OnClick", "return setValue()");

            UTel.Attributes.Add("Value", " ");
            UTel.Attributes.Add("OnFocus", "if(this.value==' ') {this.value=''}");
            UTel.Attributes.Add("OnBlur", "if(this.value==''){this.value=' '}");

            if (_Id != 0) { ShowInfo(_Id); }
        }
        //GetMenuString();
    }


    public void ShowInfo(int _NId)
    {
        using (Entities mydb = new Entities())
        {
            User employee = mydb.User.FirstOrDefault(P => P.UID == _NId);
            UTrueName.Text = employee.UTrueName;
            UTel.Text = employee.UTel;
           // CheckBox1.Checked = Convert.ToBoolean(employee.IsBoss);
         //   CheckBox1.Text = CheckBox1.Checked ? " " : "是";
            //Ujifen.Text = employee.Ujifen.ToString();
            //UTelOther.Text = employee.UTelOther;
            //UPwd.Text = DESEncrypt.Decrypt(employee.UPwd);
            //UState.SelectedValue = employee.UState.ToString();
        


            employeeAreaBind();
        }
    }
    private void employeeAreaBind()
    {
        Entities db = new Entities();
        ddlEmployee.DataTextField = "Sname";
        ddlEmployee.DataValueField = "SID";
        ddlEmployee.DataSource = db.S_Store.ToList();
        ddlEmployee.DataBind();
        //ddlEmployee.Items.Insert(0, "请选择");
    }

    //protected void GetMenuString()
    //{
    //    List<LeftMenu> menus = LeftMenu.Load();
    //    StringBuilder sb = new StringBuilder();
    //    Entities db = new Entities();
    //    var stores = db.S_Store;
    //    foreach (var store in stores)
    //    {
    //        sb.AppendFormat("<ul class=\"sec_menu\" menuid=\"{0}\">\r\n", store.SID);
    //        sb.Append("<li  class=\"li_menu\" style=\"float:left;margin-right: 10px;width:217px;list - style - type: none;\"><input type=checkbox  name=\"miao_power\" class=\"checkall checkbox-inline i-checks\"  value=\"");
    //        sb.Append(store.SID + "\"");

    //        if (miao_power != "" && miao_power != null)
    //        {
    //            if (miao_power.IndexOf("," + store.SID + ",") > -1)
    //                sb.Append(" checked");
    //        }
    //        sb.Append("/>");
    //        sb.AppendFormat(" {0} </li>\r\n", store.Sname);
    //    }
    //    sb.Append("</ul><br>\r\n");
    //    //ltMasterSetting.Text = sb.ToString();
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
        Entities db = new Entities();
        {
            User epy = new User();
            if (_Id != 0)
                epy = db.User.FirstOrDefault(P => P.UID == _Id);

            string Uname = UTrueName.Text.Trim();
            string Utel = UTel.Text.Trim();
            //if (Utel.Length != 11 || Utel[0] != '1')
            //{
            //    Response.Write("<script> var success = 3;</script>");
            //}
            //else
            //{
            var _epy = db.User.SingleOrDefault(p => (p.UTrueName == Uname) && _ID == "");
            if (_epy != null)
            {
                Response.Write("<script> var success = 1;</script>");
            }
            else
            {
                //if (!string.IsNullOrEmpty(Request.Form["miao_power"]))
                //{
                //    miao_power = "," + Request.Form["miao_power"].ToString() + ",";
                //}
                //epy.MLeaderPower = miao_power;
                //epy.MLeader = CheckBox1.Checked ? true : false;
                epy.UTrueName = Uname;
                epy.UTel = Utel;
             

                //送积分//
            

                if (USharejifen.Text.Trim() != "0")
                {
                   
                }

                if (_Id == 0)
                {
                    db.User.Add(epy);
                    db.SaveChanges();
                    Response.Write("<script> var success = 2;</script>");
                }
                else
                {
                    db.SaveChanges();
                    Response.Write("<script> var success = 2;</script>");

                }
            }
            //}
        }
    }

    /// <summary>
    /// 绑定类别DropDownList控制
    /// </summary>
    /// /// <param name="firstItemTxt">第一项显示的文字</param>
    /// <param name="ddl">要绑定的DropDownList控件</param>

  
}